package com.jiang.mall.mallorder.web;

import com.jiang.mall.mallorder.service.OrderService;
import com.jiang.mall.mallorder.vo.OrderConfirmVo;
import com.jiang.mall.mallorder.vo.OrderSubmitVo;
import com.jiang.mall.mallorder.vo.SubmitOrderResponseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.concurrent.ExecutionException;

@Controller
public class OrderWebController {

    @Autowired
    OrderService orderService;

    @GetMapping("/toTrade")
    public String toTrader (Model model) throws ExecutionException, InterruptedException {
        OrderConfirmVo confirmVo = orderService.confirmOrder();
        model.addAttribute("orderConfirmData", confirmVo);
        return "confirm";
    }

    @PostMapping("/submitOrder")
    public String submitOrder(OrderSubmitVo vo, Model model) {
        try {
            SubmitOrderResponseVo submitOrderResponseVo = orderService.submitOrder(vo);
            if(submitOrderResponseVo.getCode() == 0) {
                model.addAttribute("submitOrderResp", submitOrderResponseVo);
                return "pay";
            }else {
                return "redirect:http://order.gulimall.com/toTrade";
            }
        } catch (Exception e) {
            return "redirect:http://order.gulimall.com/toTrade";
        }
    }
}
